﻿/**

 * Copyright (c) 2015-2016, FastDev 刘强 (fastdev@163.com) & Quincy.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OF.Cache2.Entity
{
    public class HeadTailLink<T> where T : class
    {
        internal LinkItem<T> head = null;
        internal LinkItem<T> tail = null;

        private void AddEmptyLinkItem(LinkItem<T> item)
        {
            item.prev = null;
            item.next = null;
            head = item;
            tail = item;
        }

        public void Add(LinkItem<T> item)
        {
            if (head == null)
            {
                AddEmptyLinkItem(item);
            }
            else
            {
                head.prev = item;
                item.next = head;
                item.prev = null;
                head = item;
            }
        }

        public void MoveItemHead(LinkItem<T> item)
        {
            if (head == null)
            {
                AddEmptyLinkItem(item);
                return;
            }

            if (item.prev == null)
            {
                return;
            }
            else
            {
                if (item.next == null)
                {
                    item.prev.next = null;
                    tail = item.prev;
                }
                else
                {
                    LinkItem<T> itemPrev = item.prev;
                    LinkItem<T> itemNext = item.next;
                    itemPrev.next = itemNext;
                    itemNext.prev = itemPrev;
                }
                item.prev = null;
                item.next = head;
                head.prev = item;
                head = item;
            }
        }

        public void RemoveItem(LinkItem<T> item)
        {
            if (item.prev == null)
            {
                if (item == head)
                {
                    RemoveHead();
                }
                else
                {
                    item.prev = null;
                    item.next = null;
                }
            }
            else if (item.next == null)
            {
                if (item == tail)
                {
                    RemoveTail();
                }
                else
                {
                    item.prev = null;
                    item.next = null;
                }
            }
            else
            {
                RemoveMiddleItem(item);
            }
        }

        private void RemoveMiddleItem(LinkItem<T> item)
        {
            LinkItem<T> itemPrev = item.prev;
            LinkItem<T> itemNext = item.next;
            itemPrev.next = itemNext;
            itemNext.prev = itemPrev;
            item.prev = null;
            item.next = null;
        }

        private T RemoveHead()
        {
            if (head == null)
            {
                return null;
            }

            var oldHead = head;
            if (head.next == null)
            {
                head = null;
                tail = null;
            }
            else
            {
                head.next.prev = null;
                head = head.next;
            }
            oldHead.prev = null;
            oldHead.next = null;
            return oldHead.Data;
        }


        public T RemoveTail()
        {
            if (tail == null)
            {
                return null;
            }

            var oldTail = tail;
            if (tail.prev == null)
            {
                head = null;
                tail = null;
            }
            else
            {
                tail.prev.next = null;
                tail = tail.prev;
            }
            oldTail.prev = null;
            oldTail.next = null;
            return oldTail.Data;
        }
    }
}
